// const baseUrl = 'https://api.dongfresh.com/' //默认接口域名https://dp.phpcaff.cn/
// const baseUrl = 'http://106.52.8.132:8089/' //默认接口域名https://dp.phpcaff.cn/
// const baseUrl = 'http://192.168.104.159:8080/' //默认接口域名https://dp.phpcaff.cn/
// const baseUrl = '' //默认接口域名https://dp.phpcaff.cn/
// let baseUrl = ''
// let token = 'c7f95931-f02c-4bfe-9dbd-b1418a3dcfe8'

// #ifdef H5

// #endif
// #ifdef APP-PLUS || MP-WEIXIN
// baseUrl = 'http://106.52.8.132:8089/'
// #endif
import { baseUrl } from "./setting.js";
import store from '../store'
// #ifdef MP-WEIXIN
// baseUrl = 'https://api.dongfresh.com/' //默认接口域名https://dp.phpcaff.cn/
// #endif
// export {
// 	BASE_URL,
// 	WEB_URL
// }
var methods = {
	// BASE_URL = 'https://dp.yjqwl.cn/';
	// WEB_URL = 'https://dp.yjqwl.cn/';
	// post请求
	post(url, data, loading) {
		if (loading) { //是否显示加载
			uni.showLoading({
				title: ''
			})
		}
		var promise = new Promise((resolve, reject) => {
			// resolve == 成功回调
			// reject == 失败回调
			var that = this
			let token = store.state.token || uni.getStorageSync('token')
			uni.request({
				url: baseUrl + url,
				data: data,
				method: "POST",
				header: {
					token: token,
					"content-type": 'application/json'
				},
				success(res) {
					if (res.data.code == 1) {
						// 成功
						if (loading) {
							uni.hideLoading()
						}
						resolve(res.data)
					} else if (res.data.code == 401) {
						resolve(res.data)
						store.commit('logout')
						// uni.showLoading({
						// 	title: '登录失效，请重新登录...'
						// })
						if (loading) {
							uni.hideLoading()
						}
					} else {
						if (loading) {
							uni.hideLoading()
						}
						resolve(res.data)
					}
				}
			})
		})
		return promise
	},
	post1(url, data, loading) {
		var promise = new Promise((resolve, reject) => {
			if (loading) { //是否显示加载
				uni.showLoading({
					title: ''
				})
			}
			// resolve == 成功回调
			// reject == 失败回调
			var that = this
			let token = store.state.token || uni.getStorageSync('token')
			uni.request({
				url: baseUrl + url,
				data: data,
				method: "POST",
				header: {
					token: token,
					"content-type": 'application/json'
				},
				success(res) {
					uni.hideLoading()
					if (res.data.code == 1) {
						// 成功
						resolve(res.data)
					} else if (res.data.code == 401) {
						resolve(res.data)
						store.commit('logout')
						// uni.showLoading({
						// 	title: '登录失效，请重新登录...'
						// })
						uni.hideLoading()
					} else {
						resolve(res.data)
					}
				},
				fail(err) {
				}
			})
		})
		return promise
	},
	// get请求
	get(url, data, loading) {
		if (!loading) { //是否显示加载
			uni.showLoading({
				title: ''
			})
		}
		var promise = new Promise((resolve, reject) => {
			// resolve == 成功回调
			// reject == 失败回调
			var that = this
			let token = store.state.token || uni.getStorageSync('token')
			uni.request({
				url: baseUrl + url,
				data: data,
				method: "GET",
				header: {
					token: token,
					"content-type": 'application/json'
				},
				success(res) {
					uni.hideToast();
					if (res.data.code == 1) {
						// 成功
						resolve(res.data)
					} else if (res.data.code == 401) {
						resolve(res.data)
						store.commit('logout')
						uni.showLoading({
							title: '登录失效，请重新登录...'
						})
						uni.hideLoading()
					} else {
						resolve(res.data)
					}
				}
			})
		})
		return promise
	},
	// 提示信息
	msg(title, duration = 1500, mask = false, icon = 'none') {
		uni.showToast({
			title,
			duration,
			mask,
			icon
		});
	},
	// 页面跳转
	href(url) {
		uni.navigateTo({
			url: url
		});
	},
	// 一秒后返回
	oneBack() {
		setTimeout(function () {
			uni.navigateBack({
				delta: 1 // 返回上 1 页
			})
		}, 1000)
	},
	// 复制到剪贴板
	copy(text, msg) {
		uni.setClipboardData({
			data: text,
			success: function () {
				uni.showToast({
					title: msg
				});
			}
		});
	},
	// 储存数据
	set(name, data) {
		uni.setStorageSync(name, data)
	},
	// 获取数据
	// get(name) {
	// 	return uni.getStorageSync(name);
	// },
	// 删除数据
	del(name) {
		uni.removeStorageSync(name);
	},
	// 清除所有本地缓存
	clear() {
		uni.clearStorage()
	},
	// 验证手机号是否正确
	isPhoneNumber(str) {
		var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
		if (!myreg.test(str)) {
			return false;
		} else {
			return true;
		}
	},
	// 上传图片
	chooseImg() {

		return new Promise((resolve, reject) => {
			uni.chooseImage({
				count: 6, //默认9
				sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
				sourceType: ['album', 'camera'], //从相册选择
				success: function (res) {
					uni.showLoading({
						title: '上传中...'
					})
					uni.uploadFile({
						url: baseUrl + 'api/v1/upload/upload_files',
						filePath: res.tempFilePaths[0],
						name: 'file',
						header: {
							token: uni.getStorageSync('token')
						},
						success: function (res) {
							uni.hideLoading()
							if (res.statusCode == 200) {
								resolve(JSON.parse(res.data));
							} else {
								resolve(JSON.parse(res.data));
							}
						},
					})
				}
			});

		});
	},
	// 上传视频
	chooseVideo(url, name, type) {
		var url = baseUrl + url;
		return new Promise((resolve, reject) => {
			uni.chooseVideo({
				count: 1,
				sourceType: ['camera', 'album'],
				success: function (res) {
					upload(res.tempFiles[0].path)
				}
			});
		});
	},
	// 微信小程序上传文件
	chooseFile() {
		uni.chooseMessageFile({
			type: 'file',
			success(res) {
				if (res.tempFiles[0].type != 'file') {
					uni.showToast({
						title: '请选择文件类型上传',
						icon: 'none'
					})
					return
				}
				this.upload(res.tempFiles[0].path)
			}
		})
	},
	// 上传文件
	upload(file) {
		uni.showLoading({
			title: '上传中...'
		})
		var promise = new Promise((resolve, reject) => {
			uni.uploadFile({
				url: baseUrl + 'api/common/upload',
				filePath: file,
				name: 'file',
				header: {
					token: uni.getStorageSync('token')
				},
				success: function (res) {
					uni.hideLoading()
					if (res.statusCode == 200) {
						resolve(JSON.parse(res.data));
					} else {
						resolve(JSON.parse(res.data));
					}
				},
			})
		})
		return promise;
	},
	// 时间戳转时间格式
	timeFormat(time, type, interval) {
		let int = interval || '-'
		var skr = new Date(time)
		let y = skr.getFullYear()
		let M = skr.getMonth() + 1
		let d = skr.getDate()

		let h = skr.getHours()
		let m = skr.getMinutes()
		let s = skr.getSeconds()

		if (type == 'day' || type == '') {
			return y + int + m + int + d
		}
		if (type == 'second') {
			return y + int + M + int + d + ' ' + h + int + m + int + s
		}
	},
	// 检测是否登录
	isLogin() {
		if (uni.getStorageSync('token')) {
			return true
		} else {
			return false
		}
	},

	// 支付
	Payment(paydata, type) {
		// 微信小程序支付
		// #ifdef MP-WEIXIN
		var promise = new Promise((resolve, reject) => {
			uni.requestPayment({
				provider: 'wxpay',
				timeStamp: String(paydata.timeStamp),
				nonceStr: paydata.nonceStr,
				package: paydata.package,
				signType: paydata.signType,
				paySign: paydata.paySign,
				success: function (res) {
					// 支付成功
					resolve(res)
				},
				fail: function (err) {
					// 支付失败
					reject(err)
				}
			});
		})
		return promise
		// #endif
		// APP支付
		// #ifdef APP-PLUS
		var promise = new Promise((resolve, reject) => {
			uni.requestPayment({
				provider: type || 'wxpay', //wxpay alipay 支付方式
				orderInfo: paydata, //微信、支付宝订单数据 【注意微信的订单信息，键值应该全部是小写，不能采用驼峰命名】
				success: function (res) {
					resolve(res)
				},
				fail: function (err) {
					reject(res)
				}
			});
		})
		return promise
		// #endif
	},
	//

}

const myFunctions = {
	install: function (Vue, options) {
		for (var key in methods) {
			Vue.prototype[key] = methods[key];
		}
	}
};
export default myFunctions